<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\User;
use App\Http\Models\UserAuth;
use App\Http\Models\StatisticsUser;

class UserAuthController extends Controller
{
    use ModelForm;
    protected $headerStr = '用户认证';
    protected $is_status = null;

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        $this->is_status = request('is_status');
        if (isset($_GET['user_id'])) {
            $this->is_status = null;
        }
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserAuth::class, function (Grid $grid) {
            if (!is_null($this->is_status) && $this->is_status == 0) {
                $grid->model()->select('users_auth.user_id', 'users_auth.created_at', 'users.avatar', 'users.name', 'users.phone', 'users_auth.status', 'users_auth.id')
                    ->leftjoin('users', 'users.id', '=', 'users_auth.user_id')
                    ->orderBy('users_auth.status', 'asc')
                    ->where('users_auth.status', '=', '0')
                    ->orderBy('id', 'desc');
            } else {
                $grid->model()->select('users_auth.user_id', 'users_auth.created_at', 'users.avatar', 'users.name', 'users.phone', 'users_auth.status', 'users_auth.id')
                    ->leftjoin('users', 'users.id', '=', 'users_auth.user_id')
                    ->orderBy('users_auth.status', 'asc')
                    ->orderBy('id', 'desc');
            }
            $grid->id('编号')->sortable();
            $grid->user_id('用户ID(昵称)')->display(function () {
                return $this->user_id . '(' . $this->name . ')';
            })->sortable();
            $grid->avatar('头像')->display(function ($avatar) {
                return user_image_url($avatar);
            })->image(40, 40);
            $grid->phone('手机号');
            $grid->status('状态')->display(function ($value) {
                return $value == 1 ? '<span class=" label label-success">审核通过</span>' : ($value == 2 ? '<span class=" label label-warning">审核被拒</span>' : '<span class=" label label-default">审核中</span>');
            });
            $grid->created_at('提交时间');
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                //$filter->equal('status', '审核状态')->select(['0' => '未审核', '1' => '审核通过', '2' => '审核被拒']);
                $filter->where(function ($query) {
                    if ($this->input == 0) {
                        $query->whereRaw("`users_auth`.`status` = 0");
                    } elseif ($this->input == 1) {
                        $query->whereRaw("`users_auth`.`status` = 1");
                    } elseif ($this->input == 2) {
                        $query->whereRaw("`users_auth`.`status` = 2");
                    } else {

                    }
                }, '审核状态')->select(['0' => '未审核', '1' => '审核通过', '2' => '审核被拒']);
                $filter->equal('user_id', '用户ID');
            });

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                if (method_exists($actions, 'disableView')) {
                    $actions->disableView();
                }
            });
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->paginate(env('Admin_Limit'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserAuth::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->select('user_income', '收入状况')->options(UserAuth::get_user_income());
            $form->text('real_name', '真实姓名');
            $form->text('id_number', '身份证号')->rules('required');
            $form->image('id_image_just', '身份证正面');
            $form->image('id_image_back', '身份证反面');
            $form->image('income_prove', '收入证明'); //channel_info
            $form->textarea('channel_info', '渠道信息')->rows(10);
            $form->image('channel_prove', '渠道证明照片');
            $form->select('status', '审核状态')->options(['1' => '审核通过', '2' => '审核被拒']);
            $form->text('refuse', '拒绝原因');
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
            $form->saved(function (Form $form) {
                StatisticsUser::addUserAuth(); //增加用户授权数量
            });
        });
    }
}